<?php
namespace app\api\controller\contact;

use think\Controller;

class Contact extends Controller{

    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Contact.Type');

        $this->ContactModel = model('Contact.Contact');
    
    }

    // 类型
    public function type(){
        if($this->request->isPost()){

            //查询分类
            $Type = $this->TypeModel->select();
            

            $this->success('类型数据', null, $Type);
            exit;
        }
    }

    // 添加
    public function addcontact(){
        if($this->request->isPost()){
            $param = $this->request->param('');

            // 数组转换成字符串
            $param['region'] = implode(',',$param['region']) ? implode(',',$param['region']) : '';

            
            $list = $this->ContactModel->where(['nickname' => $param['nickname']])->select();

            if($list){

                $this->error('已注册该名字，请重新输入');
                exit;
            }

            //组装数据
            $data = [
                'userid'=>$param['userid'],
                'typeid'=>$param['typeid'],
                'nickname'=>$param['nickname'],
                'phone'=>$param['phone'],
                'remark'=>$param['remark'],
                'gender'=>$param['gender'],
                'region'=>$param['region']
            ];


            //开始插入数据库 
            $result = $this->ContactModel->validate('common/Contact/Contact')->save($data);

            if($result === FALSE)
            {
                // code = 0
                // getError 返回操作的结果
                $this->error($this->ContactModel->getError());
                exit;
            }else
            {
                // code = 1
                $this->success('添加成功');
                exit;
            }
        }
    }

    //首页列表
    public function index(){

        if($this->request->isPost()){

            $typeid = $this->request->param('typeid', 0);
            $keywords = $this->request->param('keywords', '');
            $userid = $this->request->param('userid', 0);

            $where = [];

            //分类
            $type = $this->TypeModel->find($typeid);

            //有分类查询分类的产品，没有分类查询全部
            if($type)
            {
                $where['typeid'] = $typeid;
            }

            if(!empty($keywords))
            {
                $where['nickname|remark'] = ['like',"%$keywords%"];
            }

            if($userid){
                $where['userid'] = $userid;
            }

            //获取商品列表
            $list = $this->ContactModel->where($where)->order('id','DESC')->select();


            if($list === FALSE)
            {
                $this->error($this->ContactModel->getError());
                exit;
            }else
            {
                $this->success('查询数据成功', null, $list);
                exit;
            }

        }
    }

    //详情
    public function info(){
        if($this->request->isPost()){

            $typeid = $this->request->param('typeid', 0);

            //名片信息 
            $info = $this->ContactModel->with(['type'])->find($typeid);

            $info['region'] = explode(',', $info['region']);
            
            $this->success('详细信息', null, $info);
            exit;
        }
    }

    //编辑
    public function update() 
    {
        if($this->request->isPost())
        {
            $param = $this->request->param('');
            
            $param['region'] = implode(',',$param['region']) ? implode(',',$param['region']) : '';

            $info = $this->ContactModel->find($param['id']);

            if(!$info){
                $this->error('找不到该名片信息！');
                exit;
            }


            $list = $this->ContactModel->where(['nickname' => $param['nickname']])->select();

            foreach($list as $item){
                if($param['id'] != $item['id']){
                    $this->error('已注册该名字，请重新输入');
                    break;
                }
            }

            //组装数据
            $data = [
                'id' => $param['id'],
                'userid'=>$param['userid'],
                'typeid'=>$param['typeid'],
                'nickname'=>$param['nickname'],
                'phone'=>$param['phone'],
                'remark'=>$param['remark'],
                'gender'=>$param['gender'],
                'region'=>$param['region']
            ];

            $result = $this->ContactModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->ContactModel->getError());
                exit;
            }else
            {
                $this->success('编辑成功');
                exit;
            }
        }
    }

}
